package com.csl.jsonpreference.util;

import android.os.Looper;
import android.util.Log;

import java.util.HashMap;

/**
 * 作者：liuchangsong on 2016/6/20 13:27
 * 邮箱：cs_lius@126.com
 */
public class CLog {
    //开关非final, 可smali中改, 输出更多日志
    private static boolean LOG_ANDROID = true;
    private static final String LOG_HEAD = "csl_";

    private static final HashMap<String, Long> times = new HashMap<>();


    public static void d(Object obj, String msg) {
        if (LOG_ANDROID) {
            Log.e(LOG_HEAD + obj.getClass().getSimpleName(), msg);
        }

    }
    public static void d(String msg) {
        if (LOG_ANDROID) {
            Log.e(LOG_HEAD + "test", msg);
        }

    }

    public static void d(String TAG, String msg) {
        if (LOG_ANDROID) {
            Log.e(LOG_HEAD + TAG, msg);
        }

    }

    /**
     * 本地测试, 无需写入文件记录
     *
     * @param TAG
     * @param msg
     */
    public static void dNoFile(String TAG, String msg) {
        if (LOG_ANDROID) {
            Log.e(LOG_HEAD + TAG, msg);
        }
    }


    /**
     * 输出当前线程与调用状态
     *
     * @param TAG
     */
    public static void pState(String TAG, String msg) {
//        if(true) {
        if (LOG_ANDROID) {
            StackTraceElement[] trace = new Throwable().getStackTrace();
            StringBuilder builder = new StringBuilder();
            if (msg != null) {
                builder.append(msg + "\n");
            }
            StackTraceElement st;
            for (int i = 1; i < trace.length; i++) {
                st = trace[i];
                builder.append("at " + st.getClassName() + "." + st.getMethodName()
                        + "(" + st.getFileName() + ":" + st.getLineNumber() + ") \n");
            }
            Log.e(
                    LOG_HEAD + TAG + "(" +
                            (Looper.getMainLooper() == Looper.myLooper() ? "main" : "child") + ")",
                    builder.toString());
        }
    }

    public static void pState(String TAG) {
        pState(TAG, null);
    }


    public static void pTime(String TAG, String content, String timekey) {
        pTime(TAG, content, timekey, true);
    }

    public static void pTime(String TAG, String content, String timekey, boolean needReset) {
        if (times.containsKey(timekey)) {
            Log.e(TAG, content + "历时:  " + (System.currentTimeMillis() - times.get(timekey)));
        }
        if (needReset) {
            times.put(timekey, System.currentTimeMillis());
        }
    }

    public static <T> String toString(T[] data) {
        StringBuilder str = new StringBuilder();
        str.append("[");
        for (T i : data) {
            str.append(" " + i + ",");
        }
        str.append("]");
        return str.toString();
    }
    public static String toString(byte[] data) {
        StringBuilder str = new StringBuilder();
        str.append("[");
        for (byte i : data) {
            str.append(" " + i + ",");
        }
        str.append("]");
        return str.toString();
    }
//    public static String toHEXString(byte[] data) {
//
//    }
}
